package com.morn.weibo.widget;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.morn.weibo.R;

public class EditTextWithCount extends LinearLayout {

	private EditText etText;

	private TextView tvCount;

	private Button btSend;

	private AlertDialog clearDialog;

	public EditTextWithCount(Context context) {
		super(context);
		init();
	}

	public EditTextWithCount(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public EditTextWithCount(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	private void init() {
		LayoutInflater layoutInflater = LayoutInflater.from(getContext());
		layoutInflater.inflate(R.layout.view_edit_text_with_count, this, true);
		etText = (EditText) findViewById(R.id.etText);
		tvCount = (TextView) findViewById(R.id.tvCount);
		btSend = (Button) findViewById(R.id.btSend);
		etText.addTextChangedListener(new TextWatcher() {

			private int strCount = 140;

			private Toast toast;

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				strCount = 140 - s.length();
				tvCount.setText(strCount + "");

			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				btSend.setEnabled(!TextUtils.isEmpty(etText.getText()));
			}

			@Override
			public void afterTextChanged(Editable s) {
				if (strCount < 0) {
					s.delete(140, s.length());
					toast.show();
				}

			}
		});

		clearDialog = new AlertDialog.Builder(this.getContext())
				.setTitle(R.string.notify)
				.setMessage(R.string.clean_text)
				.setPositiveButton(R.string.ok,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								etText.setText("");
							}
						})
				.setNegativeButton(R.string.cancel,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.cancel();
							}
						}).create();
		tvCount.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				clearDialog.show();
			}
		});
	}

	public Editable getText() {
		return etText.getText();
	}

	public void setText(CharSequence text) {
		etText.setText(text);
	}

	public void setOnSendClickListener(OnClickListener onClickListener) {
		btSend.setOnClickListener(onClickListener);
	}

}
